﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.SMS.Data;
using Wicresoft.SMS.Web.App_GlobalResources;
using System.DirectoryServices;
using System.DirectoryServices.Protocols;
using System.Net;
using Wicresoft.SMS.Common;

namespace Wicresoft.SMS.Web.Controllers
{
    public class AccountController : CustomBaseController
    {
        IUserRoleService _service;
        public AccountController(IUserRoleService service)
        {
            _service = service;
        }

        //
        // GET: /account/login
        public ViewResult LogIn()
        {
            AccountLogInModel model = new AccountLogInModel();
            model.CheckRemember = false;
            model.HiddenReturnUrl = Request.QueryString.Get("ReturnUrl");
            model.InputUserName = "Yi G ZHANG";
            model.InputPassword = "huye!!234";
            ViewData.Model = model;
            return View();
        }

        //TODO
        [HttpPost]
        public ActionResult LogIn(AccountLogInModel model)
        {
            WebConfigProvider wcp = new WebConfigProvider();

            ICILHelper ich = new CILHelper(wcp.CILServer, wcp.HRConnectionString);
            var result = ich.ValidationUser(model.InputUserName, model.InputPassword);

            // var result = _service.ValidationUser(model.InputUserName, model.InputPassword);
            if (result)
            {
                InitalUserInfo(ich.GetUserInfoByCIL(model.InputUserName));

                _service.LogIn(model.InputUserName, model.CheckRemember);
                //string url = _service.GetRedirectUrl(model.InputUserName, model.CheckRemember);
                string url = model.HiddenReturnUrl;
                if (!string.IsNullOrEmpty(url))
                    return JavaScript(string.Format("top.location = \"{0}\" ", url));
                else
                    return JavaScript(string.Format("top.location = \"{0}\" ", string.Format(@"/{0}/home/index", CultureProvider.GetCurrentCultureString())));
            }
            else
            {
                ViewData.Model = model;
                ViewData.ModelState.AddModelError("", Resources.login_msg_loginfailed_summary);
            }
            return View("LogInPartial");
        }

        private void InitalUserInfo(Common.C_evw_Employee emp)
        {
            if (emp != null)
            {

                IUserService ius = new UserService(new WebConfigProvider());

                var user = ConvertCILUser2DataUser(emp, ius.GetUserItemByCIL(emp.CILX500));

                ius.CreateOrUpdateUser(user);
            }
        }

        private Data.User ConvertCILUser2DataUser(Common.C_evw_Employee emp, Data.User user)
        {
            if (user == null)
            {
                user = new User();
                user.IsActive = false;
                user.DepartmentID = 2;
            }
            user.CIL = emp.CILX500;
            user.NameCN = emp.NameCN;
            user.NameEN = emp.NameEN;
            user.Email = emp.Email;
            return user;
        }
        public ActionResult LogOut()
        {
            _service.LogOut();
            return Redirect("~/");
        }

    }
}
